#include "viewer.h"
#include <stdio.h>
#include <GL/glut.h>

using namespace std;
using namespace qglviewer;

Viewer::Viewer(QWidget *parent)
    : QGLViewer(QGLFormat(QGL::SampleBuffers |
		QGL::DoubleBuffer |
		QGL::DepthBuffer |
		QGL::Rgba |
		QGL::AlphaChannel |
		QGL::StencilBuffer),parent)
{
}

Viewer::~Viewer()
{
}

void Viewer::init()
{
    printf("init\n");
    Vec center(0.0, 0.0, 0.0);
    setSceneRadius(25.0);
    setSceneCenter(center);
    showEntireScene();

    // Setup OpenGL
    GLfloat ambient[] = { 0.3f, 0.3f, 0.3f };
    GLfloat diffuse[] = { 0.1f, 0.1f, 0.1f , 1.0f};
    GLfloat specular[] = { 0.0f, 0.0f, 0.0f , 1.0f};
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);	
    glLightfv(GL_LIGHT0, GL_SPECULAR, specular);	

    startAnimation();
}
void Viewer::draw()
{
    foreach (Object *o, _objects)
    {
	o->render();
    }
}
void Viewer::startAnimation()
{
    QGLViewer::startAnimation();
}
void Viewer::animate()
{
    // nothing spec
}

void Viewer::addObject(Object *o)
{
    _objects.push_back(o);
}

void Viewer::UpdateObjects(float *m)
{
    for (int i = 0; i < _objects.size(); ++i)
    {
	_objects[i]->setTransMatrix(&m[i * 4 * 4]);
    }
}
